//
//  DoctorListViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/9.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class DoctorListViewController: BaseViewController {
    
    private var viewModel: DoctorViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    private let departmentId: String
    
    init(departmentId: String) {
        self.departmentId = departmentId
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "新建咨询"
        setupUI()
        setupRefresh()
    }

    private lazy var tableView: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .background
        view.tableFooterView = UIView()
        view.separatorStyle = .none
        view.showsHorizontalScrollIndicator = false
        view.register(NewAdvisoryViewCell.nibForCell(),
                      forCellReuseIdentifier: NewAdvisoryViewCell.reuseIdentifier())
        return view
    }()

}

private extension DoctorListViewController {
    func setupUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func setupRefresh() {
        tableView.mj_header = refreshHeader(action: #selector(loadData))
        tableView.mj_header?.beginRefreshing()
    }
    
    func endRefresh() {
        tableView.mj_header?.endRefreshing()
    }
    
    @objc func loadData() {
        DoctorViewModel.getDoctorList(departmentId: departmentId,
                                      success:
        { models in
            self.endRefresh()
            self.viewModel = DoctorViewModel(models: models)
        }) { errorMsg in
            self.endRefresh()
            HUDTool.show(message: errorMsg)
        }
    }
}

extension DoctorListViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.numberOfRows ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: NewAdvisoryViewCell.reuseIdentifier(),
                                                 for: indexPath) as! NewAdvisoryViewCell
        if let vm = viewModel?.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        guard let vm = viewModel?.viewModel(for: indexPath) else { return 0 }
        return vm.cellHeight
    }
}

extension DoctorListViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        

    }
}
